home *** CD-ROM | disk | FTP | other *** search
/ Qoole for Quake / Qoole for Quake (USA) / Qoole for Quake (USA).bin / Tutorial / HTML / QUBE.ZIP / SRC / CMDLIB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-05  |  9.5 KB  |  652 lines

  1. /* cmdlib.c */
  2.  
  3. #include "cmdlib.h"
  4. #include "curs.h"
  5.  
  6. #define PATHSEPERATOR   '/'
  7.  
  8. /* set these before calling CheckParm */
  9. int myargc;
  10. char **myargv;
  11.  
  12. char        com_token[1024];
  13. qboolean    com_eof;
  14.  
  15. extern FILE    *LogFile;
  16.  
  17. /*
  18. ================
  19. I_FloatTime
  20. ================
  21. */
  22. double I_FloatTime (void)
  23. {
  24.     time_t    t;
  25.     
  26.     time (&t);
  27.     
  28.     return t;
  29. #if 0
  30. /* more precise, less portable */
  31.     struct timeval tp;
  32.     struct timezone tzp;
  33.     static int        secbase;
  34.  
  35.     gettimeofday(&tp, &tzp);
  36.     
  37.     if (!secbase)
  38.     {
  39.         secbase = tp.tv_sec;
  40.         return tp.tv_usec/1000000.0;
  41.     }
  42.     
  43.     return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
  44. #endif
  45. }
  46.  
  47.  
  48. /*
  49. ==============
  50. COM_Parse
  51.  
  52. Parse a token out of a string
  53. ==============
  54. */
  55. char *COM_Parse (char *data)
  56. {
  57.     int        c;
  58.     int        len;
  59.     
  60.     len = 0;
  61.     com_token[0] = 0;
  62.     
  63.     if (!data)
  64.         return NULL;
  65.         
  66. /* skip whitespace */
  67. skipwhite:
  68.     while ( (c = *data) <= ' ')
  69.     {
  70.         if (c == 0)
  71.         {
  72.             com_eof = true;
  73.             return NULL;            /* end of file; */
  74.         }
  75.         data++;
  76.     }
  77.     
  78. /* skip /* comments */
  79.     if (c=='/' && data[1] == '/')
  80.     {
  81.         while (*data && *data != '\n')
  82.             data++;
  83.         goto skipwhite;
  84.     }
  85.     
  86.  
  87. /* handle quoted strings specially */
  88.     if (c == '\"')
  89.     {
  90.         data++;
  91.         do
  92.         {
  93.             c = *data++;
  94.             if (c=='\"')
  95.             {
  96.                 com_token[len] = 0;
  97.                 return data;
  98.             }
  99.             com_token[len] = c;
  100.             len++;
  101.         } while (1);
  102.     }
  103.  
  104. /* parse single characters */
  105.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  106.     {
  107.         com_token[len] = c;
  108.         len++;
  109.         com_token[len] = 0;
  110.         return data+1;
  111.     }
  112.  
  113. /* parse a regular word */
  114.     do
  115.     {
  116.         com_token[len] = c;
  117.         data++;
  118.         len++;
  119.         c = *data;
  120.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  121.             break;
  122.     } while (c>32);
  123.     
  124.     com_token[len] = 0;
  125.     return data;
  126. }
  127.  
  128.  
  129. int Q_strncasecmp (char *s1, char *s2, int n)
  130. {
  131.     int        c1, c2;
  132.     
  133.     while (1)
  134.     {
  135.         c1 = *s1++;
  136.         c2 = *s2++;
  137.  
  138.         if (!n--)
  139.             return 0;        /* strings are equal until end point */
  140.         
  141.         if (c1 != c2)
  142.         {
  143.             if (c1 >= 'a' && c1 <= 'z')
  144.                 c1 -= ('a' - 'A');
  145.             if (c2 >= 'a' && c2 <= 'z')
  146.                 c2 -= ('a' - 'A');
  147.             if (c1 != c2)
  148.                 return -1;        /* strings not equal */
  149.         }
  150.         if (!c1)
  151.             return 0;        /* strings are equal */
  152.     }
  153.     
  154.     return -1;
  155. }
  156.  
  157. int Q_strcasecmp (char *s1, char *s2)
  158. {
  159.     return Q_strncasecmp (s1, s2, 99999);
  160. }
  161.  
  162.  
  163. char *strupr (char *start)
  164. {
  165.     char    *in;
  166.     in = start;
  167.     while (*in)
  168.     {
  169.         *in = toupper(*in);
  170.         in++;
  171.     }
  172.     return start;
  173. }
  174.  
  175. char *strlower (char *start)
  176. {
  177.     char    *in;
  178.     in = start;
  179.     while (*in)
  180.     {
  181.         *in = tolower(*in);
  182.         in++;
  183.     }
  184.     return start;
  185. }
  186.  
  187.  
  188. /*
  189. =============================================================================
  190.  
  191.                         MISC FUNCTIONS
  192.  
  193. =============================================================================
  194. */
  195.  
  196. /*
  197. =================
  198. Error
  199.  
  200. For abnormal program terminations
  201. =================
  202. */
  203. void Error (char *error, ...)
  204. {
  205.     va_list argptr;
  206.     char text[1024];
  207.  
  208.     va_start (argptr, error);
  209.     vsprintf (text, error, argptr);
  210.     va_end (argptr);
  211.  
  212. #if 0
  213.         SetBackColor(ANSI_RED);
  214.     SetForeColor(ANSI_YELLOW);
  215.     DrawFilledBox(10, 8, 60, 6);
  216.  
  217.     MoveCurs(33, 9);
  218.     CPrintf ("*** Error ***");
  219.  
  220.         MoveCurs(12, 11);
  221.     CPrintf("$f7%s", text);
  222.     MoveCurs(12, 12);
  223.     CPrintf("$f3Press any key to exit.");
  224.  
  225.     WaitKey();
  226. #endif
  227.  
  228.     CfPrintf(LogFile, "");
  229.     CfPrintf(LogFile, "*** Error ***");
  230.     CfPrintf(LogFile, "%s", text);
  231.  
  232.     fprintf(LogFile, "-------------------------------------------\n\n");
  233.         fclose(LogFile);
  234.  
  235.         InitText();
  236.  
  237.     printf("*** Error ***\n");
  238.         printf("Aborting Qbsp compile.\n");
  239.     printf("%s\n", text);
  240.  
  241.         exit (1);
  242. }
  243.  
  244.  
  245. /*
  246. =================
  247. CheckParm
  248.  
  249. Checks for the given parameter in the program's command line arguments
  250. Returns the argument number (1 to argc-1) or 0 if not present
  251. =================
  252. */
  253. int CheckParm (char *check)
  254. {
  255.     int             i;
  256.  
  257.     for (i = 1;i<myargc;i++)
  258.     {
  259.         if ( !Q_strcasecmp(check, myargv[i]) )
  260.             return i;
  261.     }
  262.  
  263.     return 0;
  264. }
  265.  
  266.  
  267.  
  268. /*
  269. ================
  270. filelength
  271. ================
  272. */
  273. int filelength (FILE *f)
  274. {
  275.     int        pos;
  276.     int        end;
  277.  
  278.     pos = ftell (f);
  279.     fseek (f, 0, SEEK_END);
  280.     end = ftell (f);
  281.     fseek (f, pos, SEEK_SET);
  282.  
  283.     return end;
  284. }
  285.  
  286.  
  287. FILE *SafeOpenWrite (char *filename)
  288. {
  289.     FILE    *f;
  290.  
  291.     f = fopen(filename, "wb");
  292.  
  293.     if (!f)
  294.         Error ("Error opening %s: %s",filename,strerror(errno));
  295.  
  296.     return f;
  297. }
  298.  
  299. FILE *SafeOpenRead (char *filename)
  300. {
  301.     FILE    *f;
  302.  
  303.     f = fopen(filename, "rb");
  304.  
  305.     if (!f)
  306.         Error ("Error opening %s: %s",filename,strerror(errno));
  307.  
  308.     return f;
  309. }
  310.  
  311.  
  312. void SafeRead (FILE *f, void *buffer, int count)
  313. {
  314.     if ( fread (buffer, 1, count, f) != count)
  315.         Error ("File read failure");
  316. }
  317.  
  318.  
  319. void SafeWrite (FILE *f, void *buffer, int count)
  320. {
  321.     if (fwrite (buffer, 1, count, f) != count)
  322.         Error ("File read failure");
  323. }
  324.  
  325.  
  326.  
  327. /*
  328. ==============
  329. LoadFile
  330. ==============
  331. */
  332. int    LoadFile (char *filename, void **bufferptr)
  333. {
  334.     FILE    *f;
  335.     int    length;
  336.     void    *buffer;
  337.  
  338.     f = SafeOpenRead (filename);
  339.     length = filelength (f);
  340.     buffer = malloc (length+1);
  341.     ((char *)buffer)[length] = 0;
  342.     SafeRead (f, buffer, length);
  343.     fclose (f);
  344.  
  345.     *bufferptr = buffer;
  346.     return length;
  347. }
  348.  
  349.  
  350. /*
  351. ==============
  352. SaveFile
  353. ==============
  354. */
  355. void    SaveFile (char *filename, void *buffer, int count)
  356. {
  357.     FILE    *f;
  358.  
  359.     f = SafeOpenWrite (filename);
  360.     SafeWrite (f, buffer, count);
  361.     fclose (f);
  362. }
  363.  
  364.  
  365.  
  366. void DefaultExtension (char *path, char *extension)
  367. {
  368.     char    *src;
  369. /* */
  370. /* if path doesn't have a .EXT, append extension */
  371. /* (extension should include the .) */
  372. /* */
  373.     src = path + strlen(path) - 1;
  374.  
  375.     while (*src != PATHSEPERATOR && src != path)
  376.     {
  377.         if (*src == '.')
  378.             return;                 /* it has an extension */
  379.         src--;
  380.     }
  381.  
  382.     strcat (path, extension);
  383. }
  384.  
  385.  
  386. void DefaultPath (char *path, char *basepath)
  387. {
  388.     char    temp[128];
  389.  
  390.     if (path[0] == PATHSEPERATOR)
  391.         return;                   /* absolute path location */
  392.     strcpy (temp,path);
  393.     strcpy (path,basepath);
  394.     strcat (path,temp);
  395. }
  396.  
  397.  
  398. void    StripFilename (char *path)
  399. {
  400.     int             length;
  401.  
  402.     length = strlen(path)-1;
  403.     while (length > 0 && path[length] != PATHSEPERATOR)
  404.         length--;
  405.     path[length] = 0;
  406. }
  407.  
  408. void    StripExtension (char *path)
  409. {
  410.     int             length;
  411.  
  412.     length = strlen(path)-1;
  413.     while (length > 0 && path[length] != '.')
  414.     {
  415.         length--;
  416.         if (path[length] == '/')
  417.             return;        /* no extension */
  418.     }
  419.     if (length)
  420.         path[length] = 0;
  421. }
  422.  
  423.  
  424. /*
  425. ====================
  426. Extract file parts
  427. ====================
  428. */
  429. void ExtractFilePath (char *path, char *dest)
  430. {
  431.     char    *src;
  432.  
  433.     src = path + strlen(path) - 1;
  434.  
  435. /* */
  436. /* back up until a \ or the start */
  437. /* */
  438.     while (src != path && *(src-1) != PATHSEPERATOR)
  439.         src--;
  440.  
  441.     memcpy (dest, path, src-path);
  442.     dest[src-path] = 0;
  443. }
  444.  
  445. void ExtractFileBase (char *path, char *dest)
  446. {
  447.     char    *src;
  448.  
  449.     src = path + strlen(path) - 1;
  450.  
  451. /* */
  452. /* back up until a \ or the start */
  453. /* */
  454.     while (src != path && *(src-1) != PATHSEPERATOR)
  455.         src--;
  456.  
  457.     while (*src && *src != '.')
  458.     {
  459.         *dest++ = *src++;
  460.     }
  461.     *dest = 0;
  462. }
  463.  
  464. void ExtractFileExtension (char *path, char *dest)
  465. {
  466.     char    *src;
  467.  
  468.     src = path + strlen(path) - 1;
  469.  
  470. /* */
  471. /* back up until a . or the start */
  472. /* */
  473.     while (src != path && *(src-1) != '.')
  474.         src--;
  475.     if (src == path)
  476.     {
  477.         *dest = 0;    /* no extension */
  478.         return;
  479.     }
  480.  
  481.     strcpy (dest,src);
  482. }
  483.  
  484.  
  485. /*
  486. ==============
  487. ParseNum / ParseHex
  488. ==============
  489. */
  490. int ParseHex (char *hex)
  491. {
  492.     char    *str;
  493.     int    num;
  494.  
  495.     num = 0;
  496.     str = hex;
  497.  
  498.     while (*str)
  499.     {
  500.         num <<= 4;
  501.         if (*str >= '0' && *str <= '9')
  502.             num += *str-'0';
  503.         else if (*str >= 'a' && *str <= 'f')
  504.             num += 10 + *str-'a';
  505.         else if (*str >= 'A' && *str <= 'F')
  506.             num += 10 + *str-'A';
  507.         else
  508.             Error ("Bad hex number: %s",hex);
  509.         str++;
  510.     }
  511.  
  512.     return num;
  513. }
  514.  
  515.  
  516. int ParseNum (char *str)
  517. {
  518.     if (str[0] == '$')
  519.         return ParseHex (str+1);
  520.     if (str[0] == '0' && str[1] == 'x')
  521.         return ParseHex (str+2);
  522.     return atol (str);
  523. }
  524.  
  525.  
  526.  
  527. /*
  528. ============================================================================
  529.  
  530.                     BYTE ORDER FUNCTIONS
  531.  
  532. ============================================================================
  533. */
  534.  
  535. #ifdef _SGI_SOURCE
  536. #define    __BIG_ENDIAN__
  537. #endif
  538.  
  539. #ifdef __BIG_ENDIAN__
  540.  
  541. short   LittleShort (short l)
  542. {
  543.     byte    b1,b2;
  544.  
  545.     b1 = l&255;
  546.     b2 = (l>>8)&255;
  547.  
  548.     return (b1<<8) + b2;
  549. }
  550.  
  551. short   BigShort (short l)
  552. {
  553.     return l;
  554. }
  555.  
  556.  
  557. int    LittleLong (int l)
  558. {
  559.     byte    b1,b2,b3,b4;
  560.  
  561.     b1 = l&255;
  562.     b2 = (l>>8)&255;
  563.     b3 = (l>>16)&255;
  564.     b4 = (l>>24)&255;
  565.  
  566.     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  567. }
  568.  
  569. int    BigLong (int l)
  570. {
  571.     return l;
  572. }
  573.  
  574.  
  575. float    LittleFloat (float l)
  576. {
  577.     union {byte b[4]; float f;} in, out;
  578.     
  579.     in.f = l;
  580.     out.b[0] = in.b[3];
  581.     out.b[1] = in.b[2];
  582.     out.b[2] = in.b[1];
  583.     out.b[3] = in.b[0];
  584.     
  585.     return out.f;
  586. }
  587.  
  588. float    BigFloat (float l)
  589. {
  590.     return l;
  591. }
  592.  
  593.  
  594. #else
  595.  
  596.  
  597. short   BigShort (short l)
  598. {
  599.     byte    b1,b2;
  600.  
  601.     b1 = l&255;
  602.     b2 = (l>>8)&255;
  603.  
  604.     return (b1<<8) + b2;
  605. }
  606.  
  607. short   LittleShort (short l)
  608. {
  609.     return l;
  610. }
  611.  
  612.  
  613. int    BigLong (int l)
  614. {
  615.     byte    b1,b2,b3,b4;
  616.  
  617.     b1 = l&255;
  618.     b2 = (l>>8)&255;
  619.     b3 = (l>>16)&255;
  620.     b4 = (l>>24)&255;
  621.  
  622.     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  623. }
  624.  
  625. int    LittleLong (int l)
  626. {
  627.     return l;
  628. }
  629.  
  630. float    BigFloat (float l)
  631. {
  632.     union {byte b[4]; float f;} in, out;
  633.     
  634.     in.f = l;
  635.     out.b[0] = in.b[3];
  636.     out.b[1] = in.b[2];
  637.     out.b[2] = in.b[1];
  638.     out.b[3] = in.b[0];
  639.     
  640.     return out.f;
  641. }
  642.  
  643. float    LittleFloat (float l)
  644. {
  645.     return l;
  646. }
  647.  
  648.  
  649.  
  650. #endif
  651.  
  652.